﻿// TestClientApp.Form1.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       Form1.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/05/2010
// ----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;
using System.Windows.Forms;
using HSS.ServiceModel;
using TestSharedData;
using TestVirtualService;
using HSS.Forms;

namespace TestClientApp
{
	public partial class Form1 : Form
	{
		EndpointAddress epa;

		public Form1()
		{
			InitializeComponent();
			this.epa = new EndpointAddress("http://localhost.:7708/services/TestVirtualService.TestService,TestVirtualService.svc");
			VirtualChannel<ITestService>.Initialize(epa, "chuck");
			VirtualChannel<ITestService>.PromptForCredentials += new EventHandler<GetGredentialEventArguments>(Form1_PromptForCredentials);
		}

		void Form1_PromptForCredentials(object sender, GetGredentialEventArguments e)
		{
			using (var creds = new UserCredentials())
			{
				creds.ValidateCredentials = TestValidate;
				if (creds.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					e.GetCredentialCompleted = true;
					e.UserName = creds.UserName;
					e.Password = creds.Password;
				}
			}
		}
		bool TestValidate(UserCredentials uc)
		{
			return true;
		}

		int startingThreadId;

		void RefreshData()
		{
			startingThreadId = Thread.CurrentThread.ManagedThreadId;
			this.txtStartingThread.Text = startingThreadId.ToString();
			this.txtCompletionThread.Text = "";

			VirtualChannel<ITestService>.Invoke(s => s.MoreWork("chuck"), TestCompleted);
		}

		void TestCompleted(Exception error)
		{
			this.txtCompletionThread.Text = Thread.CurrentThread.ManagedThreadId.ToString();

			if (null != error)
			{
				MessageBox.Show(error.Message);
				return;
			}

			MessageBox.Show("Starting ThreadId: [" + startingThreadId + "]; Completion ThreadId: [" + Thread.CurrentThread.ManagedThreadId + "]");

		}
		private void button1_Click(object sender, EventArgs e)
		{
			RefreshData();
		}
	}
}